﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Data.Common;
using Data.Enum;
using Data.Model;
using Data.ViewModel;
using DataAccess.AccountManage;
using DataAccess.BikeManage.logical;
using Ninject;
using WinForm.Helper;

namespace WinForm.UserManage
{
    public partial class FrmCheckUser : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly IOperate _operate = Kernel.Get<IOperate>();

        public FrmCheckUser()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 清空查询条件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRset_Click(object sender, EventArgs e)
        {
            this.txtCardNo.Clear();
            this.txtTel.Clear();
            this.txtUserName.Clear();
            this.txtUserNo.Clear();
        }

        /// <summary>
        /// 查询用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveIDInfo_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                ResultInfo<CpuAccount> cpuAccount = new ResultInfo<CpuAccount>();
                Zq58Logical zq58Logical = new Zq58Logical();
                cpuAccount = zq58Logical.ReadAccountInfo();
                if (cpuAccount.ResultNumber == ResultEnum.Success)
                {
                    ResultInfo<BorrowBike> borrowBike = zq58Logical.ReadBorrowBikeInfo();
                    if (borrowBike.ResultNumber == ResultEnum.Success)
                    {
                        ResultInfo<Decimal> totalMoney = zq58Logical.QueryPurse();
                        if (totalMoney.ResultNumber == ResultEnum.Success)
                        {
                            //初始化用户信息显示
                            lbName.Text = cpuAccount.Obj.DisplayName;
                            lbSex.Text = cpuAccount.Obj.Sex;
                            lbNation.Text = cpuAccount.Obj.Nation;
                            lbValideDate.Text = borrowBike.Obj.EndAccountDate;
                            lbAddress.Text = cpuAccount.Obj.Address;
                            lbCpuVersion.Text = cpuAccount.Obj.CpuCardVersion;
                            lbCpuNo.Text = cpuAccount.Obj.CpuCardNo;
                            lbStatus.Text = cpuAccount.Obj.CpuCardStatus;
                            lbCpuType.Text = cpuAccount.Obj.CpuCardType;
                            lbFee.Text = cpuAccount.Obj.Fee;
                            lbIdGov.Text = cpuAccount.Obj.Address;
                            lbIdNo.Text = cpuAccount.Obj.IdCardNo;
                            lbIdType.Text = cpuAccount.Obj.IdCardType;
                            lbMobile.Text = cpuAccount.Obj.Mobile;
                            lbTel.Text = cpuAccount.Obj.Phone;

                            ResultInfo<Decimal> queryMoney = zq58Logical.QueryPurse();
                            if (queryMoney.ResultNumber == ResultEnum.Success)
                            {
                                lbTotalMoney.Text = queryMoney.Obj.ToString();
                            }
                            else
                            {
                                lbTotalMoney.Text = queryMoney.Describe;
                            }

                            tabItem1.Visible = false;
                            tabItem2.Visible = true;
                        }
                        else
                        {
                            errorMessage = errorMessage + totalMoney.Describe;
                        }
                    }
                    else
                    {
                        errorMessage = errorMessage + borrowBike.Describe;
                    }
                }
                else
                {
                    errorMessage = errorMessage + cpuAccount.Describe;
                }
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + e1.Message;
            }

            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        /// 查询消费信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCheck_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            if (string.IsNullOrEmpty(txtUserName.Text) && string.IsNullOrEmpty(txtUserNo.Text) && string.IsNullOrEmpty(txtCardNo.Text) && string.IsNullOrEmpty(txtTel.Text))
            {
                Zq58Logical zq = new Zq58Logical();
                ResultInfo<CpuAccount> account = zq.ReadAccountInfo();
                if (account.ResultNumber == ResultEnum.Success)
                {
                    txtCardNo.Text = account.Obj.CpuCardId;
                    txtUserName.Text = account.Obj.DisplayName;
                    RefreshData();
                }
                else
                {
                    errorMessage = errorMessage + "请输入至少一个查询参数,或者将用户卡放在读卡器上";
                }
            }
            else
            {
                RefreshData();
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        /// 更新列表控件
        /// </summary>
        private void RefreshData()
        {
            string errorMessage = string.Empty;
            try
            {
                //Zq58Logical zq58Logical = new Zq58Logical();
                //List<DealRecord> allDealRecord = new List<DealRecord>();
                //for (int i = 1; i < 21; i++)
                //{
                //    try
                //    {
                //        ResultInfo<DealRecord> deal = zq58Logical.GetDealRecords(i);
                //        if (deal.ResultNumber == ResultEnum.Success)
                //        {
                //            allDealRecord.Add(deal.Obj);
                //        }
                //    }
                //    catch (Exception)
                //    {

                //    }
                //}
                ResultInfo<PagingDataSet<jsh_consume>> consumes = _operate.GetConsumes(xPagingBar1.PageIndex, xPagingBar1.PageSize, txtCardNo.Text, txtUserNo.Text, txtUserName.Text, txtTel.Text);
                if (consumes.ResultNumber == ResultEnum.Success)
                {
                    //组装消费展示（如果cpu卡有数据库中没有的记录，就要现将cpu卡的记录显示）
                    List<ConsumeViewForDb> views = new List<ConsumeViewForDb>();
                    //if (xPagingBar1.PageIndex == 1)//只有第一页才需要添加
                    //{
                    //    if (consumes.Obj == null || consumes.Obj.Records == null)
                    //    {
                    //        foreach (DealRecord dealRecord in allDealRecord)
                    //        {
                    //            views.Add(new ConsumeView() { ConsumeMoney = dealRecord.DealMoney.ToString(), ConsumeTime = dealRecord.DealTime, CpuId = dealRecord.CpuId, CreditMoney = dealRecord.CreditMoney.ToString() });
                    //        }
                    //    }
                    //    else
                    //    {
                    //        foreach (DealRecord dealRecord in allDealRecord)
                    //        {
                    //            DealRecord dr = dealRecord;
                    //            //if(consumes.Obj.Records.Where(c=>(c.cpuid==dr.CpuId&&c.starttime.ToString("yyyymmddhhmmss")==dr.DealTime)))
                    //        }
                    //    }
                    //}
                    int serial = 0;
                    foreach (jsh_consume c in consumes.Obj.Records)
                    {
                        serial++;
                        views.Add(new ConsumeViewForDb() { Serial = serial, ConsumeMoney = c.Fee.ToString(), StartTime = c.Starttime.ToString(), EndTime = c.Endtime.ToString(), CpuId = c.cpuid, LendPsam = c.lendpsam, BackPsam = c.backpsam, BikeId = c.bikeid, UserName = txtUserName.Text, Status = c.Status });
                    }
                    xPagingBar1.PageIndex = consumes.Obj.PageIndex;
                    xPagingBar1.PageSize = consumes.Obj.PageSize;
                    xPagingBar1.TotalRecordCount = consumes.Obj.TotalRecords;
                    xPagingBar1.Bind();
                    dgvUserComsume.DataSource = views;
                }
                else
                {
                    errorMessage = errorMessage + consumes.Describe;
                }
            }
            catch (Exception e)
            {
                errorMessage = errorMessage + e.Message;
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        /// <summary>
        /// 初始化分页控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmCheckUser_Load(object sender, EventArgs e)
        {
            xPagingBar1.PageIndex = 1;
            xPagingBar1.PageSize = 20;
            xPagingBar1.xRefresh += RefreshData;
        }

        /// <summary>
        /// 查询卡消费
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCardQuery_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                Zq58Logical zq = new Zq58Logical();
                ResultInfo<CpuAccount> account = zq.ReadAccountInfo();
                if (account.ResultNumber == ResultEnum.Success)
                {
                    txtCardNo.Text = account.Obj.CpuCardId;
                    txtUserName.Text = account.Obj.DisplayName;
                    RefreshData();

                    FrmCheckCardConsume cardConsume = new FrmCheckCardConsume(txtCardNo.Text);
                    cardConsume.StartPosition = FormStartPosition.CenterScreen;
                    cardConsume.ShowDialog();
                }
                else
                {
                    errorMessage = errorMessage + "请将用户卡放在读卡器上";
                }
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + e1.Message;
            }
            if(!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxInfo(errorMessage);
            }
        }
    }
}
